﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;

#endregion


namespace ESF.General.Network.ConfigSettings
{
	public class ConnectionTestElement : ConfigurationElement
	{
		[ConfigurationProperty("name", IsRequired = false)]
        public string name
        {
            get
            { 
                return this ["name"] as string;
            }
            set
            { 
                this ["name"] = value; 
            }
        }
		
		
		[ConfigurationProperty("IPAddress", IsRequired = false)]
        public string IPAddress
        {
            get
            { 
                return this ["IPAddress"] as string;
            }
            set
            { 
                this ["IPAddress"] = value; 
            }
        }


		[ConfigurationProperty("DNSAddress", IsRequired = false)]
        public string DNSAddress
        {
            get
            { 
                return this ["DNSAddress"] as string; 
            }
            set
            { 
                this ["DNSAddress"] = value; 
            }
        }


		[ConfigurationProperty("WSAddress", IsRequired = false)]
        public string WSAddress
        {
            get
            { 
                return this ["WSAddress"] as string; 
            }
            set
            { 
                this ["WSAddress"] = value; 
            }
        }


		[ConfigurationProperty("port", IsRequired = false)]
        public int? port
        {
            get
            { 
                return (int?) this ["port"]; 
            }
            set
            { 
                this ["port"] = value; 
            }
        }


		[ConfigurationProperty("httpMethod", IsRequired = false)]
        public string httpMethod
        {
            get
            { 
                return this ["httpMethod"] as string;
            }
            set
            { 
                this ["httpMethod"] = value; 
            }
        }


		[ConfigurationProperty("isWeb", IsRequired = false, DefaultValue=false)]
        public bool isWeb
        {
            get
            { 
                return (this ["isWeb"] == null ? false : (bool) this ["isWeb"]);  
            }
            set
            { 
                this ["isWeb"] = value; 
            }
        }


		#region Funciones

		public IPEndPoint GetIPEndPoint (out string pOriginalDestination)
		{
			IPHostEntry hostEntry;

			IPAddress address;


			if (string.IsNullOrEmpty (IPAddress) && string.IsNullOrEmpty (DNSAddress))
			{
				throw new Exception (string.Format ("No ha sido definida ni la ip ni el alias DNS para {0}", name));
			}

			if (!string.IsNullOrEmpty (IPAddress))
			{
				pOriginalDestination = IPAddress;

				if (!System.Net.IPAddress.TryParse (IPAddress, out address))
				{
					throw new Exception (string.Format ("La dirección {0} es invalida", IPAddress));
				}

				return new IPEndPoint (address, port.Value);
			}

			try
			{
				pOriginalDestination = DNSAddress;

				hostEntry = Dns.GetHostEntry (DNSAddress);

				if (hostEntry.AddressList.Count () == 0)
				{
					throw new Exception (string.Format ("La dirección por DNS {0} no fue encontrada", DNSAddress));
				}

				return new IPEndPoint (hostEntry.AddressList [0], port.Value);
			}
			catch (Exception pEx)
			{
				throw new Exception (string.Format ("La dirección por DNS {0} no fue encontrada", DNSAddress), pEx);
			}
		}

		#endregion


		#region Colecciones
		
		[ConfigurationProperty("Parameters", IsDefaultCollection = false)]
		[ConfigurationCollection(typeof(ConnectionTestElementParameterCollection),
			AddItemName = "add",
			ClearItemsName = "clear",
			RemoveItemName = "remove")]
		public ConnectionTestElementParameterCollection Parameters
		{
			get
			{
				return (ConnectionTestElementParameterCollection) base ["Parameters"];
			}
		}
		
		#endregion


		#region Funciones

		public NameValueCollection GetParameters ()
		{
			NameValueCollection items;


			if (Parameters == null) { return null; }

			items = new NameValueCollection (Parameters.Count);

			foreach (ConnectionTestElementParameter param in Parameters)
			{
				items.Add (param.name, param.value);
			}

			return items;
		}

		#endregion
	}
}
